package com.payment.notify.receive.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springside.modules.utils.mapper.JsonMapper;

import com.payment.common.dto.notify.NotifyDto;
import com.payment.notify.receive.dto.SmsReceiveDto;

/**
 * 类名：NotifyReceiveUtil.java<br>
 * 描述：<br>
 * 创建者：XINJF<br>
 * 创建日期：2018年04月25日 下午2:47:13<br>
 * 版本：1.0<br>
 * 修改者：<br>
 * 修改日期：<br>
 */
public class NotifyReceiveUtil {

	private static Logger logger = LoggerFactory.getLogger(NotifyReceiveUtil.class);

	/**
	 * 编码类型 UTF-8.
	 */
	private static final String UTF_8 = "utf-8";
	
	/**
	 * 功能描述:解析接收到的消息<br>
	 * 创建者：XINJF<br>
	 * 创建日期:2018年4月25日下午6:30:43<br>
	 * 版本：1.0<br>
	 * 修改者：<br>
	 * 修改日期：<br>
	 * @param msg
	 * @return
	 */
	public static String resolveReceiveMessage(String msg) {
		try {
			msg = URLDecoder.decode(msg, UTF_8);
//			String message = new String(EncodeUtil.decodeBase64(msg));
			String message = new String(Base64.decodeBase64(msg.getBytes()));
			if(StringUtils.isBlank(message)) {
				throw new IllegalArgumentException("接收到的消息数据异常");
			}
			return message;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 功能描述:解析SMS<br>
	 * 创建者：XINJF<br>
	 * 创建日期:2018年4月25日下午6:37:25<br>
	 * 版本：1.0<br>
	 * 修改者：<br>
	 * 修改日期：<br>
	 * @param msg
	 * @return
	 */
	public static SmsReceiveDto resolveSmsMessage(String msg) {
		final String jsonString = resolveReceiveMessage(msg);
		System.out.println("jsonString=="+jsonString);
		final SmsReceiveDto smsReceiveDto = JsonMapper.INSTANCE.fromJson(jsonString, SmsReceiveDto.class);
		return smsReceiveDto;
	}
	
	/**
	 * 功能描述:统一的消息接收自来<br>
	 * 创建者：XINJF<br>
	 * 创建日期:2018年5月10日下午4:07:53<br>
	 * 版本：1.0<br>
	 * 修改者：<br>
	 * 修改日期：<br>
	 * @param msg base64位加密的数据
	 * @param receiveDtoClass 接收的类,必须是NotifyDto 子类
	 * @return
	 */
	public static NotifyDto resolveReceiveMsg(String msg,Class<?> receiveDtoClass) {
		final String jsonString = resolveReceiveMessage(msg);
		final NotifyDto notifyDto = (NotifyDto) JsonMapper.INSTANCE.fromJson(jsonString, receiveDtoClass);
		return notifyDto;
	}
	
	public static void main(String[] args) {
//		NotifyDto notifyDto = new SmsReceiveDto();
		final String jsonString = resolveReceiveMessage("eyJub3RpZnlUeXBlIjoxLCJwaG9uZSI6IjEzNzIwODk1MjgwIiwiY29udGVudCI6IjQ1NjcifQ==");
		final NotifyDto notifyDto = JsonMapper.INSTANCE.fromJson(jsonString, SmsReceiveDto.class);
		
	}



	

}
